package com.wugenqiang.utils.date;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 
 * @Title:日期工具类
 * @Description:进行日期格式转换 时间的获取
 * @Author:wugenqiang
 * @Since:2018年5月11日上午10:23:15
 * @Version:1.1.0
 */
public class DateUtils {
	public static final int INTERVAL_DAY = 1;
	public static final int INTERVAL_WEEK = 2;
	public static final int INTERVAL_MONTH = 3;
	public static final int INTERVAL_YEAR = 4;
	public static final int INTERVAL_HOUR = 5;
	public static final int INTERVAL_MINUTE = 6;
	public static final int INTERVAL_SECOND = 7;
	/*private static final String DATETIME_YMD1 = "yyyy-MM-dd";*/
	/*private static final String DATETIME_YMD2 = "yyyy/MM/dd";*/
	private static final String DATETIME_YMDMS1 = "yyyy-MM-dd HH:mm:ss.SSS";
	/*private static final String DATETIME_YMDMS2 = "yyyy/MM/dd HH:mm:ss.SSS";*/
	/*private static final String DATETIME_YMDMS3 = "yyyy-MM-dd HH:mm:ss";*/
	/*private static final String DATETIME_YMDMS4 = "yyyy/MM/dd HH:mm:ss";*/
	private static final String DATETIME_YMDMSS = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒";
	SimpleDateFormat simpleDateTime = null;

	public DateUtils() {
		this.simpleDateTime = new SimpleDateFormat();
	} 

	/**
	 * 
	 * @param date
	 *            需要格式化的日期字符串类型
	 * @return 返回java.util 的日期时间 2018-05-10 10:13:33
	 */
	public static String DateToString1(Date date) {

		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(DATETIME_YMDMS1);
		return sdf.format(date);
	}

	/**
	 * 
	 * @param date
	 *            需要格式化的日期字符串类型
	 * @return 返回java.util 的日期时间 2018年05月10日 11时19分15秒059毫秒
	 */
	public static String DateToString2(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(DATETIME_YMDMSS);
		return sdf.format(date);

	}

	/**
	 * 
	 * @param datetime
	 *            需要格式化的日期字符串类型 2018-05-10 11:11:11.013
	 * @return 返回
	 * @throws ParseException
	 */
	public static Date StringToDate1(String datetime) throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(DATETIME_YMDMS1);
		return sdf.parse(datetime);

	}

	/**
	 * 
	 * @param datetime
	 *            需要格式化的日期字符串类型 2018年05月10日 11时19分15秒059毫秒
	 * @return 返回
	 * @throws ParseException
	 */
	public static Date StringToDate2(String datetime) throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(DATETIME_YMDMSS);
		return sdf.parse(datetime);

	}

	/**
	 * 将符合pattern格式的dateValue转换成Date
	 * 
	 * @param dateValue
	 *            样例:2018/05/10
	 * @param pattern
	 *            样例:yyyy/MM/dd
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String dateValue, String pattern) throws ParseException {
		SimpleDateFormat formatTool = new SimpleDateFormat();
		formatTool.applyPattern(pattern);
		return formatTool.parse(dateValue);
	}

	/**
	 * 获取时间戳
	 * 
	 * @return 返回当前时间
	 */
	public Timestamp getTimestamp() {
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		return timestamp;
	}
	
	public static String Timestamp1toTimestamp2(Timestamp timestamp) throws ParseException{
		SimpleDateFormat sdf1= new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);

		SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String timestamp2 = null;
		timestamp2=sdf2.format(sdf1.parse(timestamp.toString()));
		System.out.println(sdf2.format(sdf1.parse(timestamp.toString())));
		return timestamp2;
		
	}

	public static int getUnixTimeByDate(Date date) {
		return (int) (date.getTime() / 1000L);
	}

	public static int getCurrentUnixTime() {
		return getUnixTimeByDate(new Date());
	}

	public static String formatDateByUnixTime(long unixTime, String dateFormat) {
		return dateFormat(new Date(unixTime * 1000L), dateFormat);
	}

	public static Date dateFormat(String date, String dateFormat) {
		if(date == null) {
			return null;
		} else {
			SimpleDateFormat format = new SimpleDateFormat(dateFormat);
			try {
				return format.parse(date);
			} catch (Exception ignored) {
			}

			return null;
		}
	}

	public static String dateFormat(Date date, String dateFormat) {
		if(date != null) {
			SimpleDateFormat format = new SimpleDateFormat(dateFormat);
			if(date != null) {
				return format.format(date);
			}
		}

		return "";
	}

	public static Date dateAdd(int interval, Date date, int n) {
		long time = date.getTime() / 1000L;
		switch(interval) {
			case 1:
				time += (long)(n * 86400);
				break;
			case 2:
				time += (long)(n * 604800);
				break;
			case 3:
				time += (long)(n * 2678400);
				break;
			case 4:
				time += (long)(n * 31536000);
				break;
			case 5:
				time += (long)(n * 3600);
				break;
			case 6:
				time += (long)(n * 60);
				break;
			case 7:
				time += (long)n;
		}

		Date result = new Date();
		result.setTime(time * 1000L);
		return result;
	}



}
